import React from 'react';
import {
  Form,
  Input,
  Button,
  Table,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  Modal,
  message,
  notification,
  Affix,
  Upload,
  Select,
  Menu,
  Dropdown
} from 'antd';

import InnerTable from 'components/DBTable/InnerTable.js';
import ajax from 'utils/ajax';
import loginVerify from 'utils/loginVerify';

const RadioGroup = Radio.Group;
const FormItem = Form.Item;
const ButtonGroup = Button.Group;
const Option = Select.Option;

var _this;

/**
 * 内部表格组件
 */
class loanManagerInnerTable extends InnerTable {
  modalActionState={
    None:0,
    Insert:1,
    Update:2,
    Delete:3,
    View:4,
    Allot:5,
    Csth:6,
    Over:7,
    Fail:8,
  };

  constructor(props) {
    super(props);
    this.state = {
      ...this.state,
      isManage: loginVerify.getUserDataInfo().userRoles.some((value) => {
        return value.role.name == '信审管理员';
      })
    }
    this.userInfo = loginVerify.getUserDataInfo();
    this.operationId = this.userInfo.realname;
    // this.isManage = this.userInfo.userRoles.some((value) => {
    //   return value.role.name == '超级管理员';
    // })
    console.log(this.userInfo);
    console.log('user',this.userInfo);
  }
  prepareModal = () => {

    if (this.state.modalState==this.modalActionState.Allot) {
      const roleList = this.state.roleInfo;
      if(!roleList)
      return;
      const List = roleList.map((value) => {
        return (<Radio key={value.id} value={value.id}>{value.realname}</Radio>)
      });
      return (
        <RadioGroup onChange={this.onRadioChange} value={this.state.radioValue}>
        {List}
        </RadioGroup>
      );
    }
    else if (this.state.modalState==this.modalActionState.Csth) {
      return (<div>
          <h4>
            确定要回退当前订单吗？
          </h4>
          <div>
            回退订单将会把该订单回退到未分配的列表下
          </div>
        </div>)
    }
    else if (this.state.modalState==this.modalActionState.Over) {
      return (<div>
          <h4>
            确定要结案当前订单吗？
          </h4>
          <div>
            结案表示通过了初审员的审核结果，您可以在“历史结案清单”中查看
          </div>
        </div>)
    }
    else if (this.state.modalState==this.modalActionState.Fail) {
      return (<div>
          <h4>
            确定要回退当前订单吗？
          </h4>
          <div>
            回退订单将会把该订单回退到原初审员的目录下
          </div>
        </div>)
  }
  }

  onRadioChange = (e) => {
    let radioName = '';
    const roleList = this.state.roleInfo;
    roleList.filter((value) => {
      if(value.id === e.target.value){
        radioName = value.realname;
      }
    });
    this.setState({
      radioValue: e.target.value,
      radioName
    });
  }

  // renderAllotModal = () => {}


  /**
   * 点击新增按钮, 弹出一个内嵌表单的modal
   *
   * @param e
   */
  onClickAllot = (e) => {
    e.preventDefault();
    ajax.post({
      url: '/creditaudit/getUesrByRoleName',
      params: {name: '信审员'},
      success: (result) => {
        if(!result.data){
          message.error('接口请求失败');
        }
        this.setState({roleInfo: result.data});
        // console.log(result)
      }
    })
    this.setState({modalVisible: true, modalTitle: '分配', modalState: this.modalActionState.Allot ,modalForm:false});
  }

  onClickCsth = (e) => {
    e.preventDefault();
    this.setState({modalVisible: true, modalTitle: '初审退回', modalState: this.modalActionState.Csth ,modalForm:false});
  }

  onClickOver = (e) => {
    e.preventDefault();
    this.setState({modalVisible: true, modalTitle: '结案', modalState: this.modalActionState.Over ,modalForm:false});
  }

  onClickFail = (e) => {
    e.preventDefault();
    this.setState({modalVisible: true, modalTitle: '结案', modalState: this.modalActionState.Fail ,modalForm:false});
  }
//分配接口
  handleAllot = () => {
    console.log('分配接口')
    const selectedRows = this.state.selectedRows;//orderId
    const { radioValue, radioName } = this.state;
    if(radioValue){
      const arr = selectedRows.map((value) => {
        return {auditStatus: 3, userId: radioValue, orderId: value.orderId, reviewer: radioName};
      });
      const params = JSON.stringify(arr);
      ajax.post({
        url: '/creditaudit/distributionOrder',
        params: {data: params, type: 1, operationId: this.operationId},
        success: () => {
          message.success('分配成功');
          this.setState({modalVisible: false,});
          this.props.refresh();
        },
        error: () => {
          // message.error('分配失败，接口请求失败');
          this.setState({modalVisible: false,});
          this.props.refresh();
        }
      });
    }else {
      message.warn('请选择一个人');
    }
  }
// 初审退回
  handleCsth = () => {
    console.log('初审退回')
    const selectedRows = this.state.selectedRows;//orderId
    // const radioValue = this.state.radioValue;
    const arr = selectedRows.map((value) => {
      return {auditStatus: 1, orderId: value.orderId};
    });
    const params = JSON.stringify(arr);
    ajax.post({
      url: '/creditaudit/distributionOrder',
      params: {data: params, type: 2, operationId: this.operationId},
      success: () => {
        message.success('回退成功');
        this.setState({modalVisible: false,});
        this.props.refresh();
      },
      error: () => {
        // message.error('分配失败，接口请求失败');
        this.setState({modalVisible: false,});
        this.props.refresh();
      }
    });
  }
  // 结案
  handleOver = () => {
    console.log('结案')
    const selectedRows = this.state.selectedRows;//orderId
    const radioValue = this.state.radioValue;
    const arr = selectedRows.map((value) => {
      const auditConclusion = value.auditConclusion;
      return {auditStatus: 7, orderId: value.orderId, userId: this.userInfo.mixId, auditConclusion};//变成当前人的id
    });
    const params = JSON.stringify(arr);
    // console.log(params);
    ajax.post({
      url: '/creditaudit/distributionOrder',
      params: {data: params, type: 3, operationId: this.operationId},
      success: () => {
        message.success('结案成功');
        this.setState({modalVisible: false,});
        this.props.refresh();
      },
      error: () => {
        // message.error('分配失败，接口请求失败');
        this.setState({modalVisible: false,});
        this.props.refresh();
      }
    });
  }
// 退回
  handleFail = () => {
    console.log('退回')
    const selectedRows = this.state.selectedRows;//orderId
    // const radioValue = this.state.radioValue;
    const arr = selectedRows.map((value) => {
      const auditConclusion = value.auditConclusion;
     
      return {orderId: value.orderId, auditConclusion};//变成当前人的id
    });
    const params = JSON.stringify(arr);
    ajax.post({
      url: '/creditaudit/finalBack',
      params: {data: params, type: 4, operationId: this.operationId},
      success: () => {
        message.success('回退成功');
        this.setState({modalVisible: false,});
        this.props.refresh();
      },
      error: () => {
        // message.error('分配失败，接口请求失败');
        this.setState({modalVisible: false,});
        this.props.refresh();
      }
    });
  }

  handleModalOk = () => {
    const selectedRows = this.state.selectedRows;//orderId
    let tableType = this.props.tableType;

    if (this.state.modalState==this.modalActionState.Allot) {
      this.handleAllot();
    }
    else if (this.state.modalState==this.modalActionState.Csth) {
      this.handleCsth();
    }
    else if (this.state.modalState==this.modalActionState.Over) {
      this.handleOver();
    }
    else if (this.state.modalState==this.modalActionState.Fail) {
      this.handleFail();
    }
    // let type = null;
  }

  prepareToolBar = () =>{
    if(!this.state.isManage)  //   权限判断，只有信审管理员才能操作按钮
    return null;
    let isButtondisabled=false;
    let tableType = this.props.tableType;
    if(this.state.selectedRowKeys.length > 0){  // 是否选择
      isButtondisabled=true;
    }
    // if(this.state.selectedRowKeys.length > 1){  // 是否选择了多项
    //   isButtondisabled=false;
    // }
    // console.log('selectedRows',this.state.selectedRows);
      let allotName="分配";
      let csthName="初审退回";
      let overName="结案";
      let failName="回退";
      let button = [];
      switch (tableType) {
        case 2:
          button.push(<Button
            disabled={!isButtondisabled}
            key="editBtn"
            onClick={this.onClickAllot}
          ><Icon type="edit" />{allotName}</Button>);
          break;
        case 3:
          button.push(<Button
            disabled={!isButtondisabled}
            key="saveBtn"
            onClick={this.onClickCsth}
          ><Icon type="plus-circle-o" />{csthName}</Button>);
          break;
        case 4:
          button.push(<Button
            disabled={!isButtondisabled}
            key="deleteBtn"
            onClick={this.onClickOver}
          ><Icon type="delete" />{overName}</Button>);
          button.push(<Button
            disabled={!isButtondisabled}
            key="failBtn"
            onClick={this.onClickFail}
          ><Icon type="delete" />{failName}</Button>);
          break;
      
        default:
          break;
      } 
        // console.log(button)
      return (<ButtonGroup>
      {/* 注意这里, 如果schema中没有定义主键, 不允许update或delete */}
      {button}
    </ButtonGroup>)
  // }
  }
}

export default loanManagerInnerTable;
